#!/bin/bash
#
# CS221 Submission Script- Based on a CS140 submission script and several
#    similar ones.
#

# Usage:       submit 
# Comments:    This is a fairly simple script which takes in the
#              assignment number as a command-line argument and copies the
#              students submission into the class submission directory.
#              The script also creates the grades.txt file used to track
#              student grades.
#


# Initialize variables
# --------------------
CLASS_DIR=/afs/ir/class/cs221
SUBMIT_ADDRESS=cs221qa@cs.stanford.edu
MAX_FILE_SIZE=4000000
SUBMIT_BASE=$CLASS_DIR/submit
ASSIGN=NULL
ASSIGN_NUM=0

# Executable locations
# --------------------
ECHO=echo
MKDIR=/usr/pubsw/bin/mkdir
DU=/usr/pubsw/bin/du
AWK=/usr/bin/awk
TAR=/usr/pubsw/bin/tar
CP=/usr/pubsw/bin/cp
MAIL=/usr/pubsw/bin/mail
PWD=/usr/bin/pwd
SED=/usr/bin/sed
RM=/usr/pubsw/bin/rm
TEST=/usr/bin/test
FS=/usr/pubsw/bin/fs
GREP=/usr/pubsw/bin/grep
FIND=/usr/pubsw/bin/find
CHOWN=/usr/pubsw/bin/chown
HESINFO=/usr/pubsw/bin/hesinfo

# Set user variables
USERNAME1=`whoami`
TEMPDIR=/tmp/`whoami`_221submit

# Welcome
# -----------------------------
$ECHO ""
$ECHO "CS221 Submission Program"
$ECHO "-------------------------"

# Gather submission information
# -----------------------------
$ECHO "NOTE: Please enter the data asked carefully!"
$ECHO ""
$ECHO "1. LittleDog Milestone"
$ECHO "2. Vision Milestone"
$ECHO "3. LittleDog Final"
$ECHO "4. Vision Final"
$ECHO -n "Enter the item number: " 
read ASSIGN_NUM

if [ $ASSIGN_NUM = 'x' ]
then
  ASSIGN_NUM=x
fi

case $ASSIGN_NUM in
   	1) ASSIGN=littledogms;;
   	2) ASSIGN=visionms;;
    3) ASSIGN=littledogfinal;;
    4) ASSIGN=visionfinal;;
	*) $ECHO Invalid assignment number
	   exit;;
esac

SUBMITDIR=$SUBMIT_BASE/$ASSIGN/$USERNAME1

# ---------- Verify that submit directory can be found
if [ ! -d $SUBMIT_BASE/$ASSIGN ]; then
    $ECHO "ABORTING: Unable to find class submission directory."
    $ECHO ""
    exit 1
fi

# --------- Get the directory where the assignment lives
$ECHO ""
$ECHO "Step 1: Get the submission directory"
$ECHO "Please enter the directory where your assignment lives (Relative paths"
$ECHO "using . and .. but not ~/ are ok -- use . for the current directory): "
read ASSIGN_DIR

if [ ! -d $ASSIGN_DIR ]; then
    $ECHO "ABORTING: Unable to find submission directory."
    $ECHO " "
    exit 1
fi

cd $ASSIGN_DIR

$ECHO "SUCCESS"
$ECHO ""
ENTERED_DIR="$ASSIGN_DIR"
ASSIGN_DIR='.'

# ---------- Step 2: Checking submission size
$ECHO "Step 2: Checking submission size... "
if [ `$DU -s $ASSIGN_DIR| $AWK '{ print $1; }'` -gt $MAX_FILE_SIZE ]; then
    $ECHO "ERROR"
    $ECHO "ABORTING: File size is too large (max size is $MAX_FILE_SIZE). Email $SUBMIT_ADDRESS if you are having space issues"
    $ECHO ""
    exit 1
fi
$ECHO "SUCCESS"
$ECHO ""


# ---------- Step 3: Gather user information
$ECHO "Step 3: Getting user information:"

# ---------- Get group member #1's information
until [ "$ANSWER" = "Y" -o "$ANSWER" = "y" ]
do
    $ECHO ""
    $ECHO "Submitting assignment for SUNet ID: $USERNAME1"
    $ECHO -n "Please enter your real name: "
    read REALNAME1
    $ECHO -n "Please enter your group's name: "
    read GROUP

# ---------- Verify information
    $ECHO ""
    $ECHO "Please verify the following information:"
    $ECHO "Submission of $ASSIGN in $ENTERED_DIR"
    $ECHO "Submitter:  $REALNAME1 ($USERNAME1@stanford.edu)"
    $ECHO "Group: $GROUP"
    $ECHO -n "Is this information correct (y/n)? "
    read ANSWER
done
$ECHO ""

# ----------- Get partners info

$ECHO -n "Do you have any partners (y/n)? "
read PARTNERS
if [ $PARTNERS != "n" -a $PARTNERS != "N" ]; then
    until [ "$ANSWER2" = "Y" -o "$ANSWER2" = "y" ]
    do
        $ECHO -n "Please enter your first partner's SUNet ID: "
        read USERNAME2
        $ECHO -n "Please enter your first partner's real name: "
        read REALNAME2
        # ---------- Verify information
        $ECHO ""
        $ECHO "Please verify the following information:"
        $ECHO "Partner 1:  $REALNAME2 ($USERNAME2@stanford.edu)"
        $ECHO -n "Is this information correct (y/n)? "
        read ANSWER2
    done

    $ECHO ""
    $ECHO -n "Do you have another partner (y/n)? "
    read PARTNERS;
    if [ $PARTNERS != "n" -a $PARTNERS != "N" ]; then
        until [ "$ANSWER3" = "Y" -o "$ANSWER3" = "y" ]
        do
            $ECHO -n "Please enter you second partner's SUNet ID: "
            read USERNAME3
            $ECHO -n "Please enter your second partner's real name: "
            read REALNAME3
            # ---------- Verify information
            $ECHO ""
            $ECHO "Partner 2:  $REALNAME3 ($USERNAME3@stanford.edu)"
            $ECHO -n "Is this information correct (y/n)? "
            read ANSWER3
        done
    else
        USERNAME3="###none"
        REALNAME3=""
    fi

else
    USERNAME2="###none"
    REALNAME2=""
    USERNAME3="###none"
    REALNAME3=""
fi

# ---------- Step 4: Copy code to /tmp
$ECHO ""
$ECHO "Step 4: copying your entire code tree to /tmp for processing... "
#cd ..
if ($TEST -w $TEMPDIR) then
    $RM -Rf $TEMPDIR
fi

umask 0077

if { $MKDIR $TEMPDIR; } then
    umask 0022
    if { $CP -r $ASSIGN_DIR $TEMPDIR/raw; } then
        $ECHO "SUCCESS"
        $ECHO ""
    else
        $ECHO "FAILURE: bailing out..."
        $ECHO "$USERNAME1\nCan't copy code tree to /tmp" | $MAIL -s "CS221 HW #$ASSIGN_NUM submission failed" $SUBMIT_ADDRESS
        exit 1
    fi
else
    $ECHO "FAILURE: bailing out..."
    $ECHO "$USERNAME1\nCan't create tmp dir" | $MAIL -s "CS221 HW #$ASSIGN_NUM submission failed" $SUBMIT_ADDRESS
    exit 1
fi


export DATE=`/bin/date +%h.%d.%y.%H.%M.%S`
INFO_FILE=$TEMPDIR/SUBMIT_INFO.$DATE
DATE2=`/bin/date`
$RM $INFO_FILE 2> /dev/null
$ECHO "$ASSIGN" > $INFO_FILE
$ECHO "$GROUP" >> $INFO_FILE
$ECHO "$REALNAME1 ($USERNAME1@stanford.edu)" >> $INFO_FILE

if [ $USERNAME2 != "###none" ]; then
    $ECHO "$REALNAME2 ($USERNAME2@stanford.edu)" >> $INFO_FILE
fi

if [ $USERNAME3 != "###none" ]; then
    $ECHO "$REALNAME3 ($USERNAME3@stanford.edu)" >> $INFO_FILE
fi

$ECHO "----------!@#$%^&*()----------" >> $INFO_FILE
$ECHO "Submission dated:" >> $INFO_FILE
$ECHO "$DATE2" >> $INFO_FILE
$ECHO "" >> $INFO_FILE


# ---------- Step 5: Remove unnecessary files
$ECHO "Step 5: removing unnecessary files... "
cd $TEMPDIR
$RM -f `$FIND | $GREP "\~$"`
$RM -f `$FIND | $GREP "\.o$"`
$RM -f `$FIND | $GREP "\.idx"`
#if [ $ASSIGN_NUM -eq 5 -o $ASSIGN_NUM -eq 7 ]; then
#    $RM -f `$FIND | $GREP "\.a$"`
#fi

$ECHO "SUCCESS"
$ECHO ""

# ---------- Step 6: Compress files
$ECHO "Step 6: compressing your files into temporary directory... "
cd raw
if ! $TAR czf ../$USERNAME1.on.$DATE.tar.gz .; then
    $ECHO "FAILED TO CREATE TAR FILE"
    exit 1
fi

find -type f -print0 |xargs -0 sha1sum >../sha1sum_${DATE}

# ---------- Step 7: Validate
$ECHO "Step 7: validating..."
export cs221_submitting=yes
if ! /afs/ir/class/cs221/bin/validate.py $ASSIGN; then
    $ECHO "Validation failed -- aborting."
    exit 1
fi

if test x$ASSIGN = xvisionfinal; then
if test ! -e config.dat; then
    $ECHO "No file named 'config.dat' found-- aborting."
    exit 1
fi
fi
    

# ---------- Step 8: Write out the submission
$ECHO "Step 8: writing your files into submission directory... "

cd ..
$RM -rf raw

if ($TEST ! -e $SUBMITDIR)
then 
    if [ "`$MKDIR $SUBMITDIR`" ]; then
        $ECHO "FAILURE: bailing out..."
	    $ECHO "$USERNAME1\nCan't create submit dir" | $MAIL -s "CS221 HW #$ASSIGN_NUM submission failed" $SUBMIT_ADDRESS
        exit 1
    fi
fi

if { $CP * $SUBMITDIR/ ; } then
    $ECHO "SUCCESS"
    $ECHO ""
else
    $ECHO "COPYING FAILURE: bailing out..."
	$ECHO "$USERNAME1\nCan't create tar file" | $MAIL -s "CS221 HW #$ASSIGN_NUM submission failed" $SUBMIT_ADDRESS
    exit 1
fi

# ---------- Step 9: Create grade file and set AFS permissions
# Will attempt to copy over a new grade file, but will fail if user
# made a previous submission

$ECHO "Step 9: setting up submission directory... "

INFO_FILE=$TEMPDIR/group.txt
DATE=`/bin/date`
$RM $INFO_FILE 2> /dev/null
$ECHO "$ASSIGN" > $INFO_FILE
$ECHO "$GROUP" >> $INFO_FILE
$ECHO "$REALNAME1 ($USERNAME1@stanford.edu)" >> $INFO_FILE

if [ $USERNAME2 != "###none" ]; then
    $ECHO "$REALNAME2 ($USERNAME2@stanford.edu)" >> $INFO_FILE
fi

if [ $USERNAME3 != "###none" ]; then
    $ECHO "$REALNAME3 ($USERNAME3@stanford.edu)" >> $INFO_FILE
fi
$CP $INFO_FILE $SUBMITDIR/ 2> /dev/null

# Reset access rights
#$FS sa $SUBMITDIR $USERNAME1 rli
#$FS sa $SUBMITDIR system:anyuser none
#$FS sa $SUBMITDIR 1062-cs221-admins all system:administrators all $USERNAME1 rli -clear
#$ECHO "SUCCESS"
#$ECHO ""


# ---------- Step 10: Clean up /tmp
$ECHO "Step 10: cleaning up temporary files... "
cd ..
$RM -rf $TEMPDIR
$ECHO "SUCCESS"
$ECHO ""


# ---------- Print Goodbye and send E-mail
$ECHO "----------------------"
$ECHO ""
$ECHO "Submission of $ASSIGN successful!"
$ECHO "* Timestamped: $DATE"
$ECHO ""
$ECHO "IMPORTANT: if for any reason you saw Permission Denied errors"
$ECHO "           during your submissions, please email cs221qa@cs.stanford.edu for further instructions"
$ECHO ""
